//
//  AboutUsViewController.m
//  StationManager
//
//  Created by 张佳强 on 17/3/22.
//  Copyright © 2017年 Helong. All rights reserved.
//

#import "AboutUsViewController.h"
#import "DeveloperInfoViewController.h"
#import <CoreImage/CoreImage.h>
@interface AboutUsViewController ()<UITableViewDelegate,UITableViewDataSource>

//提示扫码Label
@property (nonatomic,strong) UILabel * shareLabel;
//二维码
@property (nonatomic,strong) UIImageView * imageView;
//信息列表
@property (nonatomic,strong) UITableView * tableView;

@end

@implementation AboutUsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.title = @"关于我们";
    [self setUI];
}

//设置UI
- (void)setUI {
    
    _imageView = [[UIImageView alloc]init];
    _imageView.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:_imageView];
    
    [_imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(SCREEN_WIDTH/2-65));
        make.top.equalTo(@10);
        make.width.mas_equalTo(130);
        make.height.mas_equalTo(130);
    }];
    
    [self setCode];
    
    _shareLabel = [[UILabel alloc]init];
    _shareLabel.backgroundColor = [UIColor whiteColor];
    _shareLabel.text = @"浏览器扫描二维码，您的朋友可以下载此APP";
    _shareLabel.font = [UIFont systemFontOfSize:12.0];
    _shareLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:_shareLabel];
    
    [_shareLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@35);
        make.top.equalTo(_imageView.mas_bottom).offset(5);
        make.right.equalTo(@-35);
        make.height.mas_equalTo(30);
    }];
    
    _tableView = [[UITableView alloc]init];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    [self.view addSubview:_tableView];
    
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.top.equalTo(_shareLabel.mas_bottom).offset(40);
        make.width.mas_equalTo(SCREEN_WIDTH);
        make.bottom.equalTo(@0);
    }];
}

//生成二维码
- (void)setCode {
    
    //二维码过滤器
    CIFilter *qrImageFilter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    //设置过滤器默认属性 (老油条)
    [qrImageFilter setDefaults];
    //将字符串转换成 NSdata (虽然二维码本质上是 字符串,但是这里需要转换,不转换就崩溃)
    NSData *qrImageData = [@"https://itunes.apple.com/us/app/%E6%B0%94%E8%B1%A1%E5%8F%B0%E7%AB%99%E8%BF%90%E7%BB%B4/id1234153668?l=zh&ls=1&mt=8" dataUsingEncoding:NSUTF8StringEncoding];
    //设置过滤器的 输入值  ,KVC赋值
    [qrImageFilter setValue:qrImageData forKey:@"inputMessage"];
    //取出图片
    CIImage *qrImage = [qrImageFilter outputImage];
    //但是图片 发现有的小 (27,27),我们需要放大..我们进去CIImage 内部看属性
    qrImage = [qrImage imageByApplyingTransform:CGAffineTransformMakeScale(10, 10)];
    //转成 UI的 类型
    UIImage *qrUIImage = [UIImage imageWithCIImage:qrImage];
    
    //----------------给 二维码 中间增加一个 自定义图片----------------
    //开启绘图,获取图形上下文  (上下文的大小,就是二维码的大小)
    UIGraphicsBeginImageContext(qrUIImage.size);
    //把二维码图片画上去. (这里是以,图形上下文,左上角为 (0,0)点)
    [qrUIImage drawInRect:CGRectMake(0, 0, qrUIImage.size.width, qrUIImage.size.height)];
    //再把小图片画上去
    UIImage *sImage = [UIImage imageNamed:@"logo"];

    CGFloat sImageW = 130;
    CGFloat sImageH = sImageW;
    CGFloat sImageX = (qrUIImage.size.width - sImageW) * 0.5;
    CGFloat sImgaeY = (qrUIImage.size.height - sImageH) * 0.5;
    [sImage drawInRect:CGRectMake(sImageX, sImgaeY, sImageW, sImageH)];
    //获取当前画得的这张图片
    UIImage *finalyImage = UIGraphicsGetImageFromCurrentImageContext();
    //关闭图形上下文
    UIGraphicsEndImageContext();
    //设置图片
    self.imageView.image = finalyImage;
}

#pragma mark tableView - Delegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString * iden = @"aboutUs";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:iden];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:iden];
    }
    if (indexPath.row == 0) {
        cell.textLabel.text = @"开发者信息";
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }else{
        cell.textLabel.text = @"版本：V2.2";
    }
    cell.textLabel.font = [UIFont systemFontOfSize:13.0];
    cell.backgroundColor = [UIColor colorWithRed:239/255.0 green:239/255.0 blue:244/255.0 alpha:1.0];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row == 0) {
        DeveloperInfoViewController * developerVC = [[DeveloperInfoViewController alloc]init];
        [self.navigationController pushViewController:developerVC animated:YES];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
